import axios from 'axios'

const config = {
  baseURL: 'http://market.yojoincn.com',
  // baseURL: 'http://localhost:8523',
  timeout: 5 * 1000,
  crossDomain: true
}

const _axios = axios.create(config)

_axios.interceptors.response.use(
  async res => {
    if (res.status.toString().charAt(0) === '2') {
      return res.data
    }
  }
)

/**
 * @param {string}url
 * @param {object}data
 * @param {object}params
 */
export function post (url, data = {}, params = {}) {
  return _axios({
    method: 'POST',
    url,
    data,
    params
  })
}

/**
 * @param {string} url
 * @param {object} params
 */
export function get (url, params = {}) {
  return _axios({
    method: 'get',
    url,
    params
  })
}

/**
 * @param {string} url
 * @param {object} data
 * @param {object} params
 */
export function put (url, data = {}, params = {}) {
  return _axios({
    method: 'put',
    url,
    params,
    data
  })
}

/**
 * @param {string} url
 * @param {object} params
 */
export function _delete (url, params = {}) {
  return _axios({
    method: 'delete',
    url,
    params
  })
}

export default _axios
